#pragma once

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <fstream>
#include <unistd.h>
#include <stdlib.h>

#include "../comm/util.hpp"
#include "../comm/Log.hpp"

namespace ns_model
{
    using namespace ns_util;
    using namespace ns_log;

    class Question
    {
    public:
        std::string number; // 题号
        std::string title;  // 题目
        std::string star;   // 难度
        int cpu_limit;      // 时间限制
        int mem_limit;      // 空间限制
        std::string desc;   // 题目描述
        std::string header; // 用户写入代码
        std::string tail;   // 测试用例
    };

    const std::string question_list = "./questions/questions_list.txt";
    const std::string question_path = "./questions/";

    class Model
    {
        std::unordered_map<std::string, Question> _questions;

    public:
        Model()
        {
            LoadQuestionList(question_list);
        }
        ~Model() {}

        bool LoadQuestionList(const std::string &path)
        {
            std::ifstream in(path);
            if (!in.is_open())
            {
                // 打开文件失败
                LOG(FATAL) << "加载题库失败，请检查是否存在题库文件" << "\n";
                return false;
            }

            std::string line;
            while (getline(in, line))
            {
                std::vector<std::string> content;
                StringUtil::StringSpilt(line, content, " ");
                if(content.size() == 0)
                {
                    LOG(WARNING) << "加载部分题目失败，请检查文件格式" << "\n";
                    continue;
                }

                _questions[content[0]].number = content[0];
                _questions[content[0]].title = content[1];
                _questions[content[0]].star = content[2];
                _questions[content[0]].cpu_limit = atoi(content[3].c_str());
                _questions[content[0]].mem_limit = atoi(content[4].c_str());

                // std::cout << "question_path: " << question_path << std::endl;

                FileUtil::ReadFile(question_path + content[0] + "/desc.txt", &_questions[content[0]].desc, true);
                FileUtil::ReadFile(question_path + content[0] + "/header.cpp", &_questions[content[0]].header, true);
                FileUtil::ReadFile(question_path + content[0] + "/tail.cpp", &_questions[content[0]].tail, true);
              
                // std::cout << "header.cpp: " << _questions[content[0]].header << std::endl; 
                // std::cout << "desc.txt: " << _questions[content[0]].desc << std::endl;
                // std::cout << "tail.cpp: " << _questions[content[0]].tail << std::endl;
            }

            LOG(INFO) << "加载题库成功" << "\n";
            in.close();
            return true;
        }

        bool GetAllQuestions(std::vector<Question> *out)
        {
            if (_questions.size() == 0)
            {
                // 题库为空
                LOG(ERROR) <<"用户获取题库失败" << "\n";
                return false;
            }

            for (auto key : _questions)
                (*out).push_back(key.second);
            return true;
        }

        bool GetOneQuestion(const std::string &number, Question *q)
        {
            if (_questions.find(number) == _questions.end())
            {
                // 获取题目失败
                LOG(ERROR) << "用户获取题目失败， 题目编号： " << number << "\n";
                return false;
            }

            *q = _questions[number];
            return true;
        }
    };
}